#include "DerivationTree.h"
#include "CallInstruction.h"
#include "AutoPointer.h"

StarNode::StarNode() : NodeTree_CRTP<StarNode>() { }

StarNode::StarNode(const AutoPointer<NodeTree>& _left, const AutoPointer<NodeTree>& _right) : NodeTree_CRTP<StarNode>(), left(_left), right(_right) { }

StarNode::StarNode(const StarNode& copy) : NodeTree_CRTP<StarNode>(copy), left(copy.left), right(copy.right) { }

bool StarNode::execute(Pointer<LeonardVirtualMachine> LVM) {
	right->execute(LVM);
	left->execute(LVM);
	AutoPointer<TreeExpression::Expression> first = LVM->top();//TODO order
	LVM->pop();
	AutoPointer<TreeExpression::Expression> second = LVM->top();
	LVM->pop();
	first->addLeaf(second);
	TreeExpression::CallInstruction* starCall = new TreeExpression::CallInstruction("operator*");
	starCall->setSon(first);
	AutoPointer<TreeExpression::Expression> ris(starCall);
	LVM->push(ris);
	return true;
}
